<?php
declare (strict_types=1);

namespace app\organization\controller\learn;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\learn\LearnSayVocabularyRecord as LearnSayVocabularyRecordModel;
use app\common\model\banxi\organization\Organization as OrganizationModel;
use app\common\service\AccountService;
use app\common\service\learn\LearnSayVocabularyService;
use ba\Exception;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ValidateException;

class LearnSayVocabularyRecord extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new LearnSayVocabularyRecordModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        $organizationId = $this->auth->organizationId;
        $param['query']['EQ_organizationId'] = $organizationId;
        $param['query']['EQ_isSubmit'] = 1;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->with([
            'settingInfo' => function ($query) {
                $query->withoutField('languageAbility,analysis,suggest');
            },
            'gradeLevel' => function ($query) {
                $query->field('id,name');
            },
            'wordGradeLevel' => function ($query) {
                $query->field('id,name');
            }
        ])
            ->where($where)
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit'],
            ]);
        $list = result_row($list);
        $AccountService = new AccountService();
        $account = $AccountService->wrapperAccountInfo($list['rows']);
        $list['rows'] = $account;
        $this->ok('操作成功', $list);
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->with(['settingInfo'])->where($where)->find();
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $accountInfo = (new AccountService())->getAccountInfo($info['accountId'], $info['accountType']);
        if ($accountInfo && in_array($info['accountType'], [AccountTypeEnum::STUDENT, AccountTypeEnum::TRAINER])) {
            $organizationModel = new OrganizationModel();
            if ($accountInfo['organizationId'] == 0) {
                $accountInfo['organizationBaseInfo'] = $organizationModel->getOrganizationInfo();
            } else {
                $accountInfo['organizationBaseInfo'] = $organizationModel->where('id', $accountInfo['organizationId'])->field('id,name,managerName')->find();
            }
        }
        $info['accountInfo'] = $accountInfo;
        $this->ok('操作成功', $info);
    }

    /**
     * 查询-报告
     */
    public function getReport()
    {
        $param = $this->request->param(["recordId" => 0]);
        if (!$param['recordId']) {
            $this->fail('请选择你要生成报告的测试记录');
        }
        try {
            $LearnSayVocabularyService = new LearnSayVocabularyService();
            $info = $LearnSayVocabularyService->getVocabularyRecord($param['recordId']);
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功', $info);


    }
}
